package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.vo.ResultInfo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class InvoiceServiceImpl implements InvoiceService {


    @Autowired
    private InvoiceDao  invoiceDao;

    @Autowired
    private ExportDao exportDao;


    /**
     * 分页显示
     * @param example
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Invoice> findByPage(InvoiceExample example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Invoice> list = invoiceDao.selectByExample(example);
        return new PageInfo<>(list);
    }

    /**
     * 条件查询
     * @param invoiceExample
     * @return
     */
    @Override
    public List<Invoice> findAll(InvoiceExample invoiceExample) {
        return invoiceDao.selectByExample(invoiceExample);
    }


    /**
     * 通过主键查询发票
     * @param id
     * @return
     */
    @Override
    public Invoice findById(String id) {
        return invoiceDao.selectByPrimaryKey(id);
    }


    /**
     * 新增发票
     * @param invoice
     */
    @Override
    public void save(Invoice invoice) {

        //do some calculation here:3items;

        //查询”贸易条款“

        //查询报运合同id:invoice-packing -> export

        invoiceDao.updateByPrimaryKeySelective(invoice);

    }

    /**
     * don't need it for now
     * @param invoice
     */
    @Override
    public void update(Invoice invoice) {
        invoiceDao.updateByPrimaryKey(invoice);
    }


    /**
     * 判断状态，执行删除
     * @param id
     */
    @Override
    public void delete(String id) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        if(invoice == null)
            return;

        //状态==> 能否删除 0:可以删除
        if(invoice.getState()==0)
            invoiceDao.deleteByPrimaryKey(id);

    }

    /**
     * 等价JSON={"message": 0 | 1}
     * 1 :已经生成发票
     * 0 :未生成发票
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Integer> hasCreated(String id) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        int val = invoice == null ? 0 : 1;
        Map<String, Integer> map = new HashMap<>();
        map.put("message",val);
        return map;
    }


    /**
     *
     * @param invoiceId
     * @return
     */
    @Override
    public ResultInfo deleteById(String invoiceId) {

        Invoice invoice = invoiceDao.selectByPrimaryKey(invoiceId);
        ResultInfo resultInfo = new ResultInfo();

        if(invoice == null){
            resultInfo.setStatus(false);
            resultInfo.setMessage("该发票不存在！");
            return  resultInfo;
        }


        if(invoice.getState()==1){
            resultInfo.setStatus(false);
            resultInfo.setMessage("删除失败！该发票已经生成财务报运单！");
            return resultInfo;
        }else{

            boolean flag = invoiceDao.deleteById(invoiceId) > 0;

            if(flag){
                resultInfo.setStatus(true);
                resultInfo.setMessage("发票删除成功！");
            }else{
                resultInfo.setStatus(false);
                resultInfo.setMessage("发票删除失败！");
            }
            return resultInfo;
        }
    }
}
